package com.ihr360.job.core.step;

import com.ihr360.job.core.Job;
import com.ihr360.job.core.repeat.CompletionPolicy;
import com.ihr360.job.core.step.builder.JobStepBuilder;
import com.ihr360.job.core.step.builder.SimpleStepBuilder;
import com.ihr360.job.core.step.builder.TaskletStepBuilder;
import com.ihr360.job.core.step.tasklet.Tasklet;

/**
 * Convenient entry point for building all kinds of steps. Use this as a factory for fluent builders of any step.
 *
 * @author Dave Syer
 *
 * @since 2.2
 */
public class StepBuilder extends StepBuilderHelper<StepBuilder> {

    /**
     * Initialize a step builder for a step with the given name.
     *
     * @param name the name of the step
     */
    public StepBuilder(String name) {
        super(name);
    }

    /**
     * Build a step with a custom tasklet, not necessarily item processing.
     *
     * @param tasklet a tasklet
     * @return a {@link TaskletStepBuilder}
     */
    public TaskletStepBuilder tasklet(Tasklet tasklet) {
        return new TaskletStepBuilder(this).tasklet(tasklet);
    }

    /**
     * Build a step that processes items in chunks with the size provided. To extend the step to being fault tolerant,
     * call the {@link SimpleStepBuilder#faultTolerant()} method on the builder. In most cases you will want to
     * parameterize your call to this method, to preserve the type safety of your readers and writers, e.g.
     *
     * <pre>
     * new StepBuilder(&quot;step1&quot;).&lt;Order, Ledger&gt; chunk(100).reader(new OrderReader()).writer(new LedgerWriter())
     * // ... etc.
     * </pre>
     *
     * @param chunkSize the chunk size (commit interval)
     * @return a {@link SimpleStepBuilder}
     * @param <I> the type of item to be processed as input
     * @param <O> the type of item to be output
     */
    public <I, O> SimpleStepBuilder<I, O> chunk(int chunkSize) {
        return new SimpleStepBuilder<I, O>(this).chunk(chunkSize);
    }

    /**
     * Build a step that processes items in chunks with the completion policy provided. To extend the step to being
     * fault tolerant, call the {@link SimpleStepBuilder#faultTolerant()} method on the builder. In most cases you will
     * want to parameterize your call to this method, to preserve the type safety of your readers and writers, e.g.
     *
     * <pre>
     * new StepBuilder(&quot;step1&quot;).&lt;Order, Ledger&gt; chunk(100).reader(new OrderReader()).writer(new LedgerWriter())
     * // ... etc.
     * </pre>
     *
     * @param completionPolicy the completion policy to use to control chunk processing
     * @return a {@link SimpleStepBuilder}
     * @param <I> the type of item to be processed as input
     * @param <O> the type of item to be output *
     */
    public <I, O> SimpleStepBuilder<I, O> chunk(CompletionPolicy completionPolicy) {
        return new SimpleStepBuilder<I, O>(this).chunk(completionPolicy);
    }

    /**
     * Create a new step builder that will execute a job.
     *
     * @param job a job to execute
     * @return a {@link JobStepBuilder}
     */
    public JobStepBuilder job(Job job) {
        return new JobStepBuilder(this).job(job);
    }


}
